package com.fzh.controller.back;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fzh.pojo.dto.OrderInfoLinkOrderDetailsDTO;
import com.fzh.pojo.entity.OrderInfo;
import com.fzh.pojo.entity.Product;
import com.fzh.pojo.entity.User;
import com.fzh.pojo.utils.PageUtil;
import com.fzh.pojo.utils.ResponeMessageUtil;
import com.fzh.service.OrderInfoService;
import com.fzh.service.ProductService;
import com.fzh.service.UserService;

@Controller
@RequestMapping("/back/")
public class OrderInfoController {
	
	@Autowired
	private OrderInfoService orderInfoService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private ProductService productService;
	
	/**
	 * 查询所有订单数据
	 * @param page
	 * @param limit
	 * @param f_uid
	 * @param f_pid
	 * @param status
	 * @return
	 */
	@RequestMapping("/queryAllOrderInfo")
	@ResponseBody
	public ResponeMessageUtil queryAllOrderInfo(Integer page,Integer limit,String f_uid,String f_pid,String status) {
		Integer setValue = PageUtil.getSetValue(page, limit);
		return ResponeMessageUtil.ok(orderInfoService.queryOrderInfoCounts(f_uid,f_pid,status), orderInfoService.queryAllOrderInfo(setValue,limit,f_uid,f_pid,status));
	}
	
	/**
	 * 查询用户的信息用于页面内下拉框
	 * @return
	 */
	@RequestMapping("/queryAllUserWitchOrderIndoSelect")
	@ResponseBody
	public List<User> queryAllUserWitchOrderIndoSelect(){
		return userService.queryAllUserWitchOrderIndoSelect();
	}
	
	/**
	 * 查询出所有的商品，展示到订单列表下拉框
	 * @return
	 */
	@RequestMapping("/queryAllProductWitchOrderIndoSelect")
	@ResponseBody
	public List<Product> queryAllProductWitchOrderIndoSelect(){
		return productService.queryAllProductWitchImagesSelect();
	}
	
	/**
	 * 查根据订单号查询订单详情
	 * @return
	 */
	@RequestMapping("/selectOrderDetailsAll/{orderId}")
	@ResponseBody
	public OrderInfoLinkOrderDetailsDTO selectOrderDetailsAll(@PathVariable("orderId")String orderId){
		OrderInfoLinkOrderDetailsDTO orderInfoLinkOrderDetailsDTO = orderInfoService.selectOrderDetailsAll(orderId);
		return orderInfoLinkOrderDetailsDTO;
		
	}
	
	/**
	 * 添加一个订单
	 * @param orderInfo
	 * @return
	 */
	@RequestMapping("/addOrderInfo")
	@ResponseBody
	public Boolean addOrderInfo(OrderInfo orderInfo) {
		//当添加订单时获取该订单里链接的商品，把商品内的能否删除修改为0（不可删除）
		productService.editProductIsDelSwitchById(orderInfo.getF_pid(), 0);
		//生成一个订单号储存到订单数据里添加到数据库
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
		String format = dateFormat.format(new Date());
		String orderId = "699"+format+"00";
		orderInfo.setOrderId(Long.parseLong(orderId));
		return orderInfoService.addOrderInfo(orderInfo);
	}
	
	/**
	 * 编辑订单信息根据id
	 * @param orderInfo
	 * @return
	 */
	@RequestMapping("/editOrderInfoById")
	@ResponseBody
	public Boolean editOrderInfoById(OrderInfo orderInfo) {
		return orderInfoService.editOrderInfoById(orderInfo);
	}
	
	/**
	 * 批量删除订单根据ids
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delAllOrderInfoByIds")
	@ResponseBody
	public Boolean delAllOrderInfoByIds(@RequestParam("ids") String ids) {
		return orderInfoService.delAllOrderInfoByIds(ids);
	}
	
	/**
	 * 根据id删除单个的订单
	 * @param id
	 * @return
	 */
	@RequestMapping("/delOrderInfoById/{id}")
	@ResponseBody
	public Boolean delOrderInfoById(@PathVariable("id") Integer id) {
		return orderInfoService.delOrderInfoById(id);
	}
	
	/**
	 * 编辑订单的状态根据id
	 * @param id
	 * @param status
	 * @return
	 */
	@RequestMapping("/editOrderInfoStatusById/{id}/{status}")
	@ResponseBody
	public Boolean editOrderInfoStatusById(@PathVariable("id") Integer id,@PathVariable("status") Integer status) {
		return orderInfoService.editOrderInfoStatusById(id,status);
	}
	
	/**
	 * 查询订单表里数据根据ids
	 * @param ids
	 * @return
	 */
	@RequestMapping("/queryAllOrderInfoStatusByIds")
	@ResponseBody
	public Boolean queryAllOrderInfoStatusByIds(@RequestParam("ids")String ids){
		return orderInfoService.queryAllOrderInfoStatusByIds(ids);
	}
	
	/**
	 * 查询商品根据id用于在订单列表内的input框内进行显示商品的个个信息
	 * @param id
	 * @return
	 */
	@RequestMapping("/queryProductByIdWitchOrderInfoInput/{id}")
	@ResponseBody
	public Product queryProductByIdWitchOrderInfoInput(@PathVariable("id") Integer id) {
		return productService.queryProductByIdWitchCartInput(id);
	}
	
	/**
	 * 批量的进行发货
	 * @return
	 */
	@RequestMapping("/editOrderInfoStatusAllIsOne")
	@ResponseBody
	public Boolean editOrderInfoStatusAllIsOne() {
		return orderInfoService.editOrderInfoStatusAllIsOne();
	}
	
}
